/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SetupFrame.java
 *
 * Created on 24-Feb-2010, 12:03:28
 */

package phd.dupenois.changeidentifiers.application.graphical.frames;

import java.awt.Dimension;
import java.io.File;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author mpd209
 */
public class SetupFrame extends javax.swing.JFrame {
    private class ChangeIdentifierSetFileFilter extends FileFilter{

        public boolean accept(File file) {
            if(file.isDirectory()) return true;
            if(file.getPath().toLowerCase().matches("^.*\\.cis$")) return true;
            return false;
        }

        @Override
        public String getDescription() {
            return "Change Identifier Set (.cis)";
        }

    }

    private class AlgorithmFileFilter extends FileFilter{

        public boolean accept(File file) {
            if(file.isDirectory()) return true;
            if(file.getPath().toLowerCase().matches("^.*\\.jar$")) return true;
            return false;
        }

        @Override
        public String getDescription() {
            return "Java Package (.jar)";
        }

    }

    private class DotPatternFilter extends FileFilter{

        public boolean accept(File file) {
            if(file.isDirectory()) return true;
            if(file.getPath().toLowerCase().matches("^.*\\.csv$")) return true;
            return false;
        }

        @Override
        public String getDescription() {
            return "Comma-Separated Values (.csv)";
        }

    }


    private class DotPatternListModel implements ListModel{
        private Vector<String> list;
        private Vector<ListDataListener> listDataListeners;
        public DotPatternListModel(){
            list = new Vector<String>();
            listDataListeners = new Vector<ListDataListener>();
        }
        public int getSize() {
            return list.size();
        }
        public boolean moveUp(String element){
            return move(element, list.indexOf(element)-1);
        }
        public boolean moveDown(String element){
            return move(element, list.indexOf(element)+1);
        }
        public boolean move(String element, int index){
            if(index<0 || index >= list.size()) return false;
            int originalIndex = list.indexOf(element);
            if(originalIndex == index) return true;
            list.remove(element);
            list.insertElementAt(element, index);
            if(originalIndex<index){
                fireContentsChangedListener(originalIndex, index);
            }else{
                fireContentsChangedListener(index, originalIndex);
            }
            return true;
        }
        public int indexOf(String element) {
            return list.indexOf(element);
        }
        public String[] toArray(){
            return list.toArray(new String[list.size()]);
        }
        public Object getElementAt(int arg0) {
            if(arg0>=0 && arg0 < list.size()) return list.get(arg0);
            return null;
        }
        public void addElement(String element){
            if(!list.contains(element)) {
                list.add(element);
            }
            fireContentsChangedListener(list.size()-1, list.size()-1);
        }
        public void removeElement(String element){
            int index = list.indexOf(element);
            list.remove(element);
            fireContentsChangedListener(index, index);
        }
        public void fireContentsChangedListener(int index0, int index1){
            ListDataListener[] arr = listDataListeners.toArray(
                    new ListDataListener[listDataListeners.size()]);
            for(ListDataListener ldl : arr){
                ldl.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index0, index1));
            }
        }
        public void addListDataListener(ListDataListener arg0) {
            listDataListeners.add(arg0);
        }

        public void removeListDataListener(ListDataListener arg0) {
            listDataListeners.remove(arg0);
        }
        
    }
    private JFileChooser changeIdentifierSetFileChooser;
    private JFileChooser algorithmFileChooser;
    private JFileChooser dotPatternChooser;
    private DotPatternListModel dotPatternListModel;
    /** Creates new form SetupFrame */
    public SetupFrame() {
        initComponents();
        changeIdentifierSetFileChooser = new JFileChooser();
        changeIdentifierSetFileChooser.setMultiSelectionEnabled(false);
        changeIdentifierSetFileChooser.setFileFilter(new ChangeIdentifierSetFileFilter());
        changeIdentifierSetFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);


        algorithmFileChooser = new JFileChooser();
        algorithmFileChooser.setMultiSelectionEnabled(false);
        algorithmFileChooser.setFileFilter(new AlgorithmFileFilter());
        algorithmFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);


        dotPatternChooser = new JFileChooser();
        dotPatternChooser.setMultiSelectionEnabled(true);
        dotPatternChooser.setFileFilter(new DotPatternFilter());
        dotPatternChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);


        Dimension size = new Dimension(500, 450);
        this.setSize(size);
        this.setMaximumSize(size);
        this.setMinimumSize(size);
        this.setPreferredSize(size);

        this.setResizable(false);
        dotPatternListModel = new DotPatternListModel();
        loadDotPatternStreamList.setModel(dotPatternListModel);
        

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        loadChangeIdentifierSetPanel = new javax.swing.JPanel();
        loadChangeIdentifierSetTextField = new javax.swing.JTextField();
        loadChangeIdentifierSetBrowseButton = new javax.swing.JButton();
        loadAlgorithmPanel = new javax.swing.JPanel();
        loadAlgorithmTextField = new javax.swing.JTextField();
        loadAlgorithmBrowseButton = new javax.swing.JButton();
        loadDotPatternStreamPanel = new javax.swing.JPanel();
        loadDotPatternStreamDisplayPanel = new javax.swing.JPanel();
        loadDotPatternStreamListPanel = new javax.swing.JPanel();
        loadDotPatternStreamScrollPane = new javax.swing.JScrollPane();
        loadDotPatternStreamList = new javax.swing.JList();
        loadDotPatternStreamListButtons = new javax.swing.JPanel();
        loadDotPatternStreamAddButton = new javax.swing.JButton();
        loadDotPatternStreamListUpButton = new javax.swing.JButton();
        loadDotPatternStreamListDownButton = new javax.swing.JButton();
        loadDotPatternStreamListRemoveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        loadChangeIdentifierSetPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Change Identifier Set"));
        loadChangeIdentifierSetPanel.setLayout(new javax.swing.BoxLayout(loadChangeIdentifierSetPanel, javax.swing.BoxLayout.LINE_AXIS));

        loadChangeIdentifierSetTextField.setEditable(false);
        loadChangeIdentifierSetPanel.add(loadChangeIdentifierSetTextField);

        loadChangeIdentifierSetBrowseButton.setText("Browse");
        loadChangeIdentifierSetBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadChangeIdentifierSetBrowseButtonActionPerformed(evt);
            }
        });
        loadChangeIdentifierSetPanel.add(loadChangeIdentifierSetBrowseButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(loadChangeIdentifierSetPanel, gridBagConstraints);

        loadAlgorithmPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Algorithm"));
        loadAlgorithmPanel.setLayout(new javax.swing.BoxLayout(loadAlgorithmPanel, javax.swing.BoxLayout.LINE_AXIS));

        loadAlgorithmTextField.setEditable(false);
        loadAlgorithmPanel.add(loadAlgorithmTextField);

        loadAlgorithmBrowseButton.setText("Browse");
        loadAlgorithmBrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadAlgorithmBrowseButtonActionPerformed(evt);
            }
        });
        loadAlgorithmPanel.add(loadAlgorithmBrowseButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(loadAlgorithmPanel, gridBagConstraints);

        loadDotPatternStreamPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Dot Pattern Stream"));
        loadDotPatternStreamPanel.setLayout(new java.awt.BorderLayout());

        loadDotPatternStreamDisplayPanel.setLayout(new java.awt.GridBagLayout());

        loadDotPatternStreamListPanel.setLayout(new java.awt.GridLayout(1, 0));

        loadDotPatternStreamScrollPane.setViewportView(loadDotPatternStreamList);

        loadDotPatternStreamListPanel.add(loadDotPatternStreamScrollPane);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        loadDotPatternStreamDisplayPanel.add(loadDotPatternStreamListPanel, gridBagConstraints);

        loadDotPatternStreamListButtons.setLayout(new java.awt.GridLayout(1, 0));

        loadDotPatternStreamAddButton.setText("Add");
        loadDotPatternStreamAddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadDotPatternStreamAddButtonActionPerformed(evt);
            }
        });
        loadDotPatternStreamListButtons.add(loadDotPatternStreamAddButton);

        loadDotPatternStreamListUpButton.setText("Up");
        loadDotPatternStreamListUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadDotPatternStreamListUpButtonActionPerformed(evt);
            }
        });
        loadDotPatternStreamListButtons.add(loadDotPatternStreamListUpButton);

        loadDotPatternStreamListDownButton.setText("Down");
        loadDotPatternStreamListDownButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadDotPatternStreamListDownButtonActionPerformed(evt);
            }
        });
        loadDotPatternStreamListButtons.add(loadDotPatternStreamListDownButton);

        loadDotPatternStreamListRemoveButton.setText("Remove");
        loadDotPatternStreamListRemoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadDotPatternStreamListRemoveButtonActionPerformed(evt);
            }
        });
        loadDotPatternStreamListButtons.add(loadDotPatternStreamListRemoveButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
        gridBagConstraints.weightx = 1.0;
        loadDotPatternStreamDisplayPanel.add(loadDotPatternStreamListButtons, gridBagConstraints);

        loadDotPatternStreamPanel.add(loadDotPatternStreamDisplayPanel, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(loadDotPatternStreamPanel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public String getAlgorithm(){
        return loadAlgorithmTextField.getText();
    }
    public String getChangeIdentifierSet(){
        return loadChangeIdentifierSetTextField.getText();
    }
    public String[] getDotPatternStream(){
        return dotPatternListModel.toArray();
    }
    private void loadChangeIdentifierSetBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadChangeIdentifierSetBrowseButtonActionPerformed
        int returnVal = changeIdentifierSetFileChooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
               loadChangeIdentifierSetTextField.setText(changeIdentifierSetFileChooser.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_loadChangeIdentifierSetBrowseButtonActionPerformed

    private void loadAlgorithmBrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadAlgorithmBrowseButtonActionPerformed
        int returnVal = algorithmFileChooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
               loadAlgorithmTextField.setText(algorithmFileChooser.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_loadAlgorithmBrowseButtonActionPerformed

    private void loadDotPatternStreamAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadDotPatternStreamAddButtonActionPerformed
        int returnVal = dotPatternChooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File[] files = dotPatternChooser.getSelectedFiles();
            for(File file : files) {
                if(file.isDirectory()){
                    File[] children = file.listFiles(new java.io.FileFilter(){
                        public boolean accept(File arg0) {
                            return dotPatternChooser.getFileFilter().accept(arg0);
                        }
                    });
                    for(File c : children){
                        if(!c.isDirectory()) dotPatternListModel.addElement(c.getAbsolutePath());
                    }
                }else{
                    dotPatternListModel.addElement(file.getAbsolutePath());
                }
            }
        }
    }//GEN-LAST:event_loadDotPatternStreamAddButtonActionPerformed

    private void loadDotPatternStreamListUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadDotPatternStreamListUpButtonActionPerformed
        Object[] selection = loadDotPatternStreamList.getSelectedValues();
        boolean moveable = true;
        int[] indices = new int[selection.length];
        for(int i=0;i<selection.length&&moveable;i++){
            moveable = dotPatternListModel.moveUp((String)selection[i]);
        }
        for(int i=0;i<selection.length;i++){
            indices[i] = dotPatternListModel.indexOf((String)selection[i]);
        }
        loadDotPatternStreamList.setSelectedIndices(indices);
    }//GEN-LAST:event_loadDotPatternStreamListUpButtonActionPerformed

    private void loadDotPatternStreamListDownButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadDotPatternStreamListDownButtonActionPerformed
        Object[] selection = loadDotPatternStreamList.getSelectedValues();
        boolean moveable = true;
        int[] indices = new int[selection.length];
        for(int i=selection.length-1;i>=0&&moveable;i--){
            moveable = dotPatternListModel.moveDown((String)selection[i]);
        }
        for(int i=selection.length-1;i>=0;i--){
            indices[i] = dotPatternListModel.indexOf((String)selection[i]);
        }
        loadDotPatternStreamList.setSelectedIndices(indices);
    }//GEN-LAST:event_loadDotPatternStreamListDownButtonActionPerformed

    private void loadDotPatternStreamListRemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadDotPatternStreamListRemoveButtonActionPerformed
        Object[] selection = loadDotPatternStreamList.getSelectedValues();
        for(Object s: selection)dotPatternListModel.removeElement((String)s);
    }//GEN-LAST:event_loadDotPatternStreamListRemoveButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton loadAlgorithmBrowseButton;
    private javax.swing.JPanel loadAlgorithmPanel;
    private javax.swing.JTextField loadAlgorithmTextField;
    private javax.swing.JButton loadChangeIdentifierSetBrowseButton;
    private javax.swing.JPanel loadChangeIdentifierSetPanel;
    private javax.swing.JTextField loadChangeIdentifierSetTextField;
    private javax.swing.JButton loadDotPatternStreamAddButton;
    private javax.swing.JPanel loadDotPatternStreamDisplayPanel;
    private javax.swing.JList loadDotPatternStreamList;
    private javax.swing.JPanel loadDotPatternStreamListButtons;
    private javax.swing.JButton loadDotPatternStreamListDownButton;
    private javax.swing.JPanel loadDotPatternStreamListPanel;
    private javax.swing.JButton loadDotPatternStreamListRemoveButton;
    private javax.swing.JButton loadDotPatternStreamListUpButton;
    private javax.swing.JPanel loadDotPatternStreamPanel;
    private javax.swing.JScrollPane loadDotPatternStreamScrollPane;
    // End of variables declaration//GEN-END:variables

}
/*
 * loadDotPatternStreamList.setDragEnabled(true);
        loadDotPatternStreamList.setDropMode(DropMode.ON);
        loadDotPatternStreamList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        loadDotPatternStreamList.setTransferHandler(new TransferHandler(){
            @Override
            public boolean canImport(TransferHandler.TransferSupport info) {
                // we only import Strings
                if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    return false;
                }

                JList.DropLocation dl = (JList.DropLocation)info.getDropLocation();
                if (dl.getIndex() == -1) {
                    return false;
                }
                return true;
            }

            @Override
            public int getSourceActions(JComponent arg0) {
                return MOVE;
            }

            @Override
            public Transferable createTransferable(JComponent c) {
                return new StringSelection((String)
                        loadDotPatternStreamList.getModel().getElementAt(
                        loadDotPatternStreamList.getSelectionModel().getLeadSelectionIndex()
                        ));
            }

            @Override
            protected void exportDone(JComponent arg0, Transferable arg1, int arg2) {

            }

            @Override
            public boolean importData(TransferSupport info) {
                if (!info.isDrop()) {
                    return false;
                }

                // Check for String flavor
                if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    return false;
                }

                Transferable t = info.getTransferable();
                String data;
                try {
                    data = (String)t.getTransferData(DataFlavor.stringFlavor);
                }
                catch (Exception e) { return false; }

                dotPatternListModel.addElement(data);
                return true;

            }




        });*/